/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package elaborate.tag_analysis.oosm.tools.builder;

import elaborate.tag_analysis.oosm.OOSM;
import elaborate.tag_analysis.oosm.OOSMElement;
import elaborate.tag_analysis.oosm.OOSMElementList;
import elaborate.tag_analysis.oosm.OOSMRule;
import elaborate.tag_analysis.oosm.OOSMSerializer;
import elaborate.tag_analysis.oosm.impl.DefaultOOSMImpl;
import elaborate.tag_analysis.oosm.impl.DefaultOOSMRuleImpl;
import elaborate.tag_analysis.oosm.impl.DefaultOOSMSerializer;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.xml.namespace.QName;

/**
 * An OOSMBuilder is a tool for building OOSM schemas.
 * @author LENDLE
 */
public class OOSMBuilder extends javax.swing.JFrame {
    private JFileChooser fileChooser=new JFileChooser(".");
    private OOSM currentOOSM=null;
    /**
     * Creates new form OOSMBuilder
     */
    public OOSMBuilder() {
        initComponents();
        
//        DefaultOOSMSerializer serializer=new DefaultOOSMSerializer();
//        /*
//        test.test1 ::= root
//        root ::= a
//        a ::= (b,c)
//        */
//        DefaultOOSMImpl impl=(DefaultOOSMImpl) serializer.createNew();
//        impl.setName(new QName("test", "test1"));
//        impl.setDescription("description");
//        
//        OOSMElement root=impl.createElement(new QName("test", "root"), null);
//        impl.setRootElement(root);
//        
//        OOSMElement a=impl.createElement(new QName("test", "a"), null);
//        
//        OOSMElement b=impl.createElement(new QName("test", "b"), null);
//        
//        OOSMElement c=impl.createElement(new QName("test", "c"), null);
//        
//        //root :: =a
//        OOSMRule rule1=new DefaultOOSMRuleImpl(root);
//        rule1.getConstructs().add(a);
//        impl.getRules().add(rule1);
//        //a ::= (b,c)
//        OOSMRule rule2=new DefaultOOSMRuleImpl(a);
//        rule2.getConstructs().add(b);
//        rule2.getConstructs().add(c);
//        OOSMElementList list1=impl.createElementList(new QName("test", "list1"), null);
//        OOSMElement d=impl.createElement(new QName("test", "d"), null);
//        OOSMElement e=impl.createElement(new QName("test", "e"), null);
//        list1.getElements().add(d);
//        list1.getElements().add(e);
//        rule2.getConstructs().add(list1);
//        impl.getRules().add(rule2);
//        
//        OOSMTreeModel model=new OOSMTreeModel(impl);
//        currentOOSM=impl;
//        this.treeOOSM.setModel(model);
        this.treeOOSM.setCellRenderer(new OOSMTreeCellRenderer());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        treeOOSM = new javax.swing.JTree();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuOpen = new javax.swing.JMenuItem();
        menuSave = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        menuExit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setPreferredSize(new java.awt.Dimension(400, 25));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 25, Short.MAX_VALUE)
        );

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        treeOOSM.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane1.setViewportView(treeOOSM);

        jMenu1.setMnemonic('F');
        jMenu1.setText("File");

        menuOpen.setMnemonic('O');
        menuOpen.setText("Open");
        menuOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuOpenActionPerformed(evt);
            }
        });
        jMenu1.add(menuOpen);

        menuSave.setMnemonic('S');
        menuSave.setText("Save");
        menuSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuSaveActionPerformed(evt);
            }
        });
        jMenu1.add(menuSave);
        jMenu1.add(jSeparator1);

        menuExit.setMnemonic('x');
        menuExit.setText("Exit");
        menuExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuExitActionPerformed(evt);
            }
        });
        jMenu1.add(menuExit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuOpenActionPerformed
        // TODO add your handling code here:
        if(this.fileChooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
            OOSMSerializer serializer=new DefaultOOSMSerializer();
            try(InputStreamReader input=new InputStreamReader(new FileInputStream(this.fileChooser.getSelectedFile()), "utf-8")){
                this.currentOOSM=serializer.createOOSM(input);
                OOSMTreeModel model=new OOSMTreeModel(currentOOSM);
                this.treeOOSM.setModel(model);
            }catch (Exception ex) {
                Logger.getLogger(OOSMBuilder.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_menuOpenActionPerformed

    private void menuSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuSaveActionPerformed
        // TODO add your handling code here:
        if(this.fileChooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){
            OOSMSerializer serializer=new DefaultOOSMSerializer();
            try(OutputStreamWriter output=new OutputStreamWriter(new FileOutputStream(this.fileChooser.getSelectedFile()), "utf-8" )){
                serializer.save(currentOOSM, output);
            } catch (Exception ex) {
                Logger.getLogger(OOSMBuilder.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_menuSaveActionPerformed

    private void menuExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExitActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_menuExitActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(OOSMBuilder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(OOSMBuilder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(OOSMBuilder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(OOSMBuilder.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new OOSMBuilder().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JMenuItem menuExit;
    private javax.swing.JMenuItem menuOpen;
    private javax.swing.JMenuItem menuSave;
    private javax.swing.JTree treeOOSM;
    // End of variables declaration//GEN-END:variables
}
